package io.renren.common.utils;

import com.alibaba.fastjson.JSON;
import io.renren.common.exception.ErrorCode;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.extern.slf4j.Slf4j;


import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Serializable;

/**
 * 响应数据
 *
 * @author mts
 */
@Slf4j
@ApiModel(value = "响应")
public class Result<T> implements Serializable {
    private static final long serialVersionUID = 1L;
    /**
     * 编码：0表示成功，其他值表示失败
     */
    @ApiModelProperty(value = "编码：0表示成功，其他值表示失败")
    private int code = 0;
    /**
     * 消息内容
     */
    @ApiModelProperty(value = "消息内容")
    private String msg = "success";
    /**
     * 响应数据
     */
    @ApiModelProperty(value = "响应数据")
    private T data;

    public Result<T> ok(T data) {
        this.setData(data);
        return this;
    }

    public boolean success(){
        return code == 0 ? true : false;
    }

    public Result<T> error() {
        this.code = ErrorCode.INTERNAL_SERVER_ERROR;
        this.msg = MessageUtils.getMessage(this.code);
        return this;
    }

    public Result<T> error(int code) {
        this.code = code;
        this.msg = MessageUtils.getMessage(this.code);
        return this;
    }

    public Result<T> error(int code, String msg) {
        this.code = code;
        this.msg = msg;
        return this;
    }

    public Result<T> error(String msg) {
        this.code = ErrorCode.INTERNAL_SERVER_ERROR;
        this.msg = msg;
        return this;
    }

    public Result<T> error(String msg, T data) {
        this.code = ErrorCode.INTERNAL_SERVER_ERROR;
        this.msg = msg;
        this.data = data;
        return this;
    }


    public static String needLogin() {
        Result result = new Result();
        result.setCode(ErrorCode.UNAUTHORIZED);
        result.setMsg("请先登录！");
        return JSON.toJSONString(result);
    }

    public static String accountLoginOnly() {
        Result result = new Result();
        result.setCode(ErrorCode.ACCOUNT_LOGIN_ONLY);
        result.setMsg("同一个浏览器只允许一个账号登录，请先退出系统后再登录！");
        return JSON.toJSONString(result);
    }

    public static String needAuthorization() {
        Result result = new Result();
        result.setCode(ErrorCode.FORBIDDEN);
        result.setMsg("拒绝访问，没权限！");
        return JSON.toJSONString(result);
    }

    /**
     * 返回数据，一般用于拦截器
     * @param response HttpServletResponse
     * @param json String
     */
    public static void returnJson(HttpServletResponse response, String json) {


        PrintWriter writer = null;
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json; charset=utf-8");
        try {
            writer = response.getWriter();
            writer.print(json);
        } catch (IOException e) {
            log.error("响应客户端请求失败...",e);
        } finally {
            if (writer != null){
                writer.close();
            }
        }
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
